
// 先是轮询一遍,然后再判断谁大谁小,
// 大的前进多的几个.  然后一起往后轮询.
;(function(){
    //return
    /**
   * Definition for singly-linked list.
   * function ListNode(val) {
   *     this.val = val;
   *     this.next = null;
   * }
   */
  
  /**
   * @param {ListNode} headA
   * @param {ListNode} headB
   * @return {ListNode}
   */
  var getIntersectionNode = function(headA, headB) {
      if(!headA||!headB) return
      let pointerA = headA
      let pointerB = headB
  
      let numA = 1
      while(pointerA.next){
          numA++
          pointerA = pointerA.next
      }
      let numB = 1
      while(pointerB.next){
          numB++
          pointerB = pointerB.next
      }
    
      if(!numA||!numB) return null;
    
      if(numA>numB){
          let n = numA - numB
          pointerA = headA
          pointerB = headB
          while(n--)pointerA = pointerA.next
      }else{
          let n = numB - numA
        
          pointerA = headA
          pointerB = headB
          while(n--)pointerB = pointerB.next
      }
    
      if(pointerA===pointerB) return pointerA
      while(pointerA.next){
          if(pointerA===pointerB) return pointerA
          pointerA = pointerA.next
          pointerB = pointerB.next
          if(pointerA===pointerB) return pointerA
      }
  
      return null
  };
  })();
  
  // 双指针
  ;(function(){
    //return
    var getIntersectionNode = function(headA, headB) {
      if (headA === null || headB === null) {
          return null;
      }
      let pA = headA, pB = headB;
      while (pA !== pB) {
          // 要么相交, 要么一起最后指向null,
          // 这两种情况都是相等了
  
          pA = pA === null ? headB : pA.next;
          pB = pB === null ? headA : pB.next;
      }
      return pA;
  };
  
  })();